/**
 * 
 */
package com.es.disped.web.controller.area;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.area.AreaService;
import com.es.disped.common.constant.RecordStatus;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.Area;
import com.es.disped.dao.model.AreaExample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Anson<br>
 *
 * Copyright by EasyShare 2019<br>
 *
 * All right reserved<br>
 *
 * Created on 下午3:41:36<br>
 *
 * 名称：AreaController.java<br>
 *
 * 描述：区域管理<br>
 */
@Api("区域管理")
@Controller 
@RequestMapping("/manage/area")
public class AreaController extends BaseController {

	@Autowired
	AreaService areaService;
	
	@ApiOperation(value="保存地区",response=MsgModel.class,httpMethod="POST",
			notes="保存地区对象，地区名称，地区联系人手机号以及地区联系人不能为空")
	@RequiresPermissions(value="sys:save")
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="area",value="地区对象",required=true) Area area)
	{
		int count=0;
		if(Optional.of(area)
				.filter(name->area.getAreaName()!=null)
				.filter(code->area.getAreaPhone()!=null)
				.filter(uid->area.getUserId()!=null)
				.isPresent())
		{
			area.setAreaId(getUUID());
			area.setAreaValid(RecordStatus.USEAGE.getValue());
			count=areaService.insertSelective(area);
		}
		return new MsgModel(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"":"保存失败", count==1?area:"") ;
	}
	
	
	
	@ApiOperation(value="更新地区",response=MsgModel.class,httpMethod="POST",
			notes="更新地区对象，其中地区编号不能为空")
	@RequiresPermissions(value="sys:update")
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="area",value="地区对象",required=true) Area area)
	{
		int count=0;
		if(Optional.of(area)
				.filter(areaId->area.getAreaId()!=null)
				.isPresent())
		{
			count=areaService.updateByPrimaryKeySelective(area);
		}
		return new MsgModel(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"":"保存失败", count==1?area:"") ;
	}
	
	
	@ApiOperation(value="删除地区",response=MsgModel.class,httpMethod="POST",notes="删除地区，需要地区编号")
	@RequiresPermissions(value="sys:delete")
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="area",value="地区对象",required=true) Area area)
	{
		int count=0;
		if(Optional.of(area)
				.filter(areaId->area.getAreaId()!=null)
				.isPresent())
		{
			count=areaService.deleteByPrimaryKey(area.getAreaId());
		}
		return new MsgModel(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"":"删除失败", count==1?area:"");
	}
	
	
	@ApiOperation(value="检索地区",response=MsgModel.class,httpMethod="POST",notes="检索地区，包括模糊检索")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="area",value="地区对象",required=false) Area area)
	{
		AreaExample listExample=new AreaExample();
		AreaExample.Criteria criteria=listExample.createCriteria();
		if(!this.isNull(area.getAreaId()))
		{
			criteria.andAreaIdEqualTo(area.getAreaId());
		}
		if(!this.isNull(area.getAreaName()))
		{
			criteria.andAreaNameLike("%"+area.getAreaName()+"%");
		}
		if(!this.isNull(area.getAreaPhone()))
		{
			criteria.andAreaPhoneLike("%"+area.getAreaPhone()+"%");
		}
		if(!this.isNull(area.getAreaAddress()))
		{
			criteria.andAreaAddressLike("%"+area.getAreaAddress()+"%");
		}
		List<Area> areas=areaService.selectByExample(listExample);
		return this.resultMsg(areas.size()>0?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				areas.size()>0?"":"地区列表为空", areas.size()>0?areas:"");
	}
	
	
	@ApiOperation(value="获取地区列表",httpMethod="POST",response=MsgModel.class,notes="获取地区信息（管理端），分页加载")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		AreaExample pageExample=new AreaExample();
		PageModel<Area> pageModel=null;
		if(Optional.of(paramMap)
		.filter(offset->paramMap.containsKey("offset")!=false)
		.filter(limit->paramMap.containsKey("limit")!=false)
		.isPresent())
		{
			pageModel=areaService.selectByExampleForOffsetPage(pageExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
		}
		return this.resultMsg(pageModel!=null?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				pageModel!=null?"":"地区列表为空", pageModel!=null?pageModel:"");
	}
}
